package com.kgcx.organization.service;

import com.github.pagehelper.PageHelper;
import com.kgcx.common.bean.RequestData;
import com.kgcx.common.constant.Cons;
import com.kgcx.common.utils.CollectionUtils;
import com.kgcx.dbutils.domain.BaseDomain;
import com.kgcx.dbutils.domain.Dict;
import com.kgcx.dbutils.service.DictService;
import com.kgcx.dbutils.utils.PageUtils;
import com.kgcx.organization.domain.DriverMsg;
import com.kgcx.organization.domain.DriverMsgType;
import com.kgcx.organization.mapper.DriverMsgMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 吴刚林
 * @date 2017/11/22
 */
@Service
public class DriverMsgService {
    @Autowired
    private DriverMsgMapper driverMsgMapper;

    @Autowired
    private DictService dictService;

    public List<DriverMsg> getDriverMsgList(RequestData rd) {

        String orderBy = rd.getOrderBy();
        if (StringUtils.isBlank(orderBy)) {
            orderBy = "create_time desc";
        }
        PageUtils.page(rd);
        PageHelper.orderBy(orderBy);
        List<DriverMsg> driverMsgList = driverMsgMapper.selectActive(BaseDomain.buildSelectDomain(rd, new DriverMsg()));
        if (CollectionUtils.isNotEmpty(driverMsgList)) {
            List<Long> kids = driverMsgList.stream().map(DriverMsg::getKid).collect(Collectors.toList());
            updateDriverMsgIsRead(kids);
        }
        return driverMsgList;
    }

    public List<DriverMsgType> getDriverMsgOverview(Long driverId) {
        List<Dict> dict = dictService.getDictByType(Dict.DRIVER_MSG_TYPE);
        List<DriverMsgType> driverMsgOverview = driverMsgMapper.getDriverMsgOverview(driverId);
        driverMsgOverview.forEach(
                d -> dict.stream().filter(
                        (di -> di.getValue().equals(d.getTypeId().toString()))
                ).findFirst().ifPresent(f ->
                        d.setTypeName(f.getName()))
        );
        return driverMsgOverview;
    }

    public Integer getDriverMsgUnreadCount(Long driverId) {

        DriverMsg driverMsg = new DriverMsg();
        driverMsg.setDriverId(driverId);
        driverMsg.setIsRead(DriverMsg.UNREAD);
        return driverMsgMapper.selectCountActive(driverMsg);
    }

    @Transactional(rollbackFor = Exception.class)
    public void addDriverMsg(DriverMsg driverMsg) {
        driverMsgMapper.insertSelective(driverMsg);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateDriverMsg(DriverMsg driverMsg) {
        driverMsgMapper.updateByPrimaryKeySelective(driverMsg);
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeDriverMsg(Long kid) {
        DriverMsg driverMsg = new DriverMsg();
        driverMsg.setKid(kid);
        driverMsg.setStatus(Cons.COMMON_STATUS_DELETE);
        driverMsgMapper.updateByPrimaryKeySelective(driverMsg);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateDriverMsgIsRead(List<Long> kids) {
        Example example = new Example(DriverMsg.class);
        example.createCriteria().andIn("kid", kids);
        DriverMsg driverMsgUpdate = new DriverMsg();
        driverMsgUpdate.setIsRead(DriverMsg.READ);
        driverMsgMapper.updateByExampleSelective(driverMsgUpdate, example);
    }

}
